package com.lsh.dao;

import com.lsh.domain.UserPraise;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * 点赞功能Dao层
 * @author Administrator
 * @date 2019/10/01 23:16
 */
@Repository
public interface PraiseDao {

    // 查询指定的评价是否存在
    @Select("SELECT IF(id IS NULL, 0, 1) as id FROM user_comments WHERE id = #{comment_id}")
    Integer findByCommentId(@Param("comment_id") String commentId);

    // 保存点赞用户与评价的关联数据
    @Insert("INSERT INTO users_comments_praise (comment_id, user_id) VALUES (#{commentId}, #{userId})")
    boolean savePraise(UserPraise userPraise);

    // 查询点赞用户与评价的关联数据是否存在
    @Select("SELECT * FROM users_comments_praise WHERE comment_id = #{comment_id} AND user_id = #{user_id}")
    @Results(value = {
            @Result(property = "commentId", column = "comment_id"),
            @Result(property = "userId", column = "user_id")
    })
    UserPraise findPraise(@Param("comment_id") String commentId, @Param("user_id") String userId);

    // 删除点赞用户与评价的关联数据
    @Delete("DELETE FROM users_comments_praise WHERE user_id = #{userId} AND comment_id = #{commentId}")
    boolean deletePraise(UserPraise userPraise);

    // 查询指定评价的点赞数
    @Select("SELECT praise_num FROM user_comments WHERE id = #{comment_id}")
    Integer findByCommentCount(@Param("comment_id") String commentId);

    // 更新指定评价的点赞数
    @Update("UPDATE user_comments SET praise_num = #{praise_num} WHERE id = #{comment_id}")
    int updateCommentCount(@Param("comment_id") String commentId, @Param("praise_num") int praiseNum);
}
